package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {


    /**
     * spring-data 会自动注入ITravelCommentRepository实现类
     * <p>
     * ITravelCommentRepository 接口继承MongoRepository
     * 拥有了 分页,排序,crud基本功能
     * 底层实现 : AOP 操作--动态代理
     */

    @Autowired
    private ITravelCommentRepository repository;

    //使用较为底层的贾琏欲执事, 实现mongodb操作, 这里用它来实现较为复杂操作
    @Autowired
    private MongoTemplate template;


    @Override
    public TravelComment saveOrUpdate(TravelComment comment) {

        //判断评论id是否为空
        if (!StringUtils.hasLength(comment.getId())) {
            comment.setCreateTime(new Date());
        }
        //评论的评论
        if (comment.getType() == TravelComment.TRAVLE_COMMENT_TYPE) {
            TravelComment travelComment = repository.findById(comment.getRefComment().getId()).get();
            comment.setRefComment(travelComment);
        }
        repository.save(comment);
        return comment;
    }


    @Override
    public Page query(TravelCommentQuery qo) {
        //查询条件
        Query query = new Query();
        if (qo.getTravelId() != -1) {
            //攻略id条件
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        //查总数
        long count = template.count(query, TravelComment.class);

        if (count == 0) {
            return Page.empty();
        }
        //select count(id) from xxx where ....
        //select * from xxx where .... order by
        //参数1:当前页,从0开始
        //参数2:每页显示条数
        //参数3:排序规则,参数a:表示倒序 参数b:哪个字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);

        //查某一页  List
        List<TravelComment> list = template.find(query, TravelComment.class);
        // PageImpl  Page接口实现类
        return new PageImpl(list, pageable, count);
    }

    @Override
    public Long getFloor(Long travelId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("travelId").is(travelId));
        return template.count(query, TravelComment.class);
    }

    @Override
    public List<TravelComment> listNewsCommends() {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));
        query.skip(0).limit(10);
        List<TravelComment> list = template.find(query, TravelComment.class, "travel_comment");
        return list;
    }

    @Override
    public TravelComment getByNewTime(Long id) throws Exception{
        List<TravelComment> all = repository.findByTravelId(id);
        //System.out.println(all);
        Date now = new Date();
        long mindifftime = 0;
        TravelComment  travelComment = new TravelComment();
        for (TravelComment base : all) {
            BeanUtils.copyProperties(travelComment,base);
            mindifftime = base.getCreateTime().getTime();
        }
        //System.out.println(mindifftime);
        //System.out.println(strategyComment.getUsername());
        for (TravelComment comment : all) {
            //System.out.println(comment.getCreateTime());
            long difftime = now.getTime() - comment.getCreateTime().getTime();
            //System.out.println(difftime);
            if (mindifftime > difftime){
                mindifftime = difftime;
                BeanUtils.copyProperties(travelComment,comment);
            }
        }
        return travelComment;
    }
}
